package com.cd.javacxy.common.aspect;

import com.cd.javacxy.common.annotation.RequireLogin;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;

/**
 * 登录拦截
 *
 * @author yanghy
 * @date 2019/11/30
 */

@Slf4j
public class RequireLoginAspect {
    @Around(value = "@annotation(com.cd.javacxy.common.annotation.RequireLogin)&&@annotation(requireLogin)")
    public Object checkLogin(ProceedingJoinPoint point, RequireLogin requireLogin) throws Throwable {
        /*RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        assert sra != null;
        HttpServletRequest request = sra.getRequest();
        HttpServletResponse response = sra.getResponse();
        HttpSession session = request.getSession();
        assert response != null;
        if (session.getAttribute(Constants.SESSION_USER_KEY) == null) {
            log.info("未登录用户");
            final String xmlHttpRequest = "XMLHttpRequest";
            final String requestedWith = "X-Requested-With";
            if (xmlHttpRequest.equalsIgnoreCase(request.getHeader(requestedWith))) {
                AjaxResponse<Object> re = AjaxResponse.builder().code(ResponseCode.LOGINTIMEOUT.getCode()).errorMsg("未登录用户").build();
                ResponseUtil.writeJavaScript(re, response);
            } else {
                //没有user信息的话进行路由重定向
                response.sendRedirect("/signin");
            }
            return null;
        } else {
            return point.proceed();
        }*/
        return point.proceed();
    }
}
